package com.woniu.controller;

import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
//import org.springframework.ai.zhipuai.ZhiPuAiChatModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;
@RestController
@RequestMapping("zhipu")
public class ZhiPuChatController {
//    @Autowired
//    private ZhiPuAiChatModel chatModel;
//
//    @GetMapping("/ai/generate")
//    public Map generate(@RequestParam(value = "message", defaultValue = "请问成都今天天气如何") String message) {
//        return Map.of("generation", this.chatModel.call(message));
//    }

//    @GetMapping(value = "/ai/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "请问成都今天天气如何") String message) {
//        var prompt = new Prompt(new UserMessage(message));
//        Flux<ChatResponse> stream = chatModel.stream(prompt);
//        return chatModel.stream(prompt).flatMapSequential(res -> Flux.just(res.getResult().getOutput().getText()));
////        return chatModel.stream(prompt);
//    }

//    @GetMapping(value = "/ai/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "请问成都今天天气如何") String message) {
//        var prompt = new Prompt(new UserMessage(message));
//        return chatModel.stream(prompt).flatMapSequential(res -> {
//            String content = res.getResult().getOutput().getText();
//            if (content == null || content.length() == 0) {
//                return Flux.just("");
//            }
//            return Flux.just(content);
//        });
    }

